package com.lyf.shop.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @ClassName DataBaseUtil
 * @Description TODO
 * @Author cos
 * @Date DATE:下午 3:08
 */
public class DataBaseUtil {
    //声明数据库连接池
    private static DruidDataSource ds;

    static {
        try {
            Properties pro = new Properties();
            pro.load(DataBaseUtil.class.getResourceAsStream("/jdbc.properties"));
            ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return
     */
    public static Connection getConnection(){
        try {
            return ds.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static DruidDataSource getDataSoucre(){
        return ds;
    }
}
